//
//  HTCommentView.m
//  TruckDriver
//
//  Created by Ting Han on 2017/10/25.
//  Copyright © 2017年 Ting Han. All rights reserved.
//

#import "HTCommentView.h"
#import "UIView+Layout.h"
#import "HTCommentBoardView.h"

@interface HTCommentView()<HTCommentBoardViewDelegate>
@property (nonatomic,strong) HTCommentBoardView *commentBoardView;
@property (nonatomic,strong) UIView *contentView;
@end

@implementation HTCommentView

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        self.frame = [UIScreen mainScreen].bounds;
        [self setupView];
    }
    return self;
}
- (void)setupView{
    self.backgroundColor = [[UIColor blackColor]colorWithAlphaComponent:0.3];
    self.contentView = [[UIView alloc]initWithFrame:self.bounds];
    self.contentView.backgroundColor =  [[UIColor blackColor]colorWithAlphaComponent:0.1];
    [self addSubview:self.contentView];
    [self.contentView addSubview:self.commentBoardView];
    [self.commentBoardView.closeButton addTarget:self action:@selector(dismissView) forControlEvents:UIControlEventTouchUpInside];
    [self.commentBoardView.commitButtom addTarget:self action:@selector(didCommitAction) forControlEvents:UIControlEventTouchUpInside];
}

- (void)freeAction{
    
}
- (HTCommentBoardView *)commentBoardView{
    if (!_commentBoardView) {
        _commentBoardView = [[NSBundle mainBundle]loadNibNamed:@"HTCommentBoardView" owner:self options:nil].firstObject;
        _commentBoardView.width = kHTScreenWidth/8*7;
        _commentBoardView.center = self.center;
        _commentBoardView.delegate = self;
    }
    return _commentBoardView;
}
- (void)didChangeHeight:(CGFloat)height{
    self.commentBoardView.height = height;
    self.commentBoardView.center = self.center;
}

- (void)didCommitAction{
    if (self.commentBoardView.selectedData.count <= 0) {
        [HTShowUtil showMessage:@"请选择评价标签"];
        return;
    }
    NSString *message = @"";
    for (NSString *data in self.commentBoardView.selectedData) {
        message = [NSString stringWithFormat:@"%@,%@",message,data];
    }
    message = [message substringFromIndex:1];
    if (self.delegate && [self.delegate respondsToSelector:@selector(didCommitComment:scroll:orderid:commentView:)]) {
       
        [self.delegate didCommitComment:message scroll: [NSString stringWithFormat:@"%ld",self.commentBoardView.selectedIndex]orderid:self.orderid commentView:self];
    }
    
}

-(void)showView{
    
    [[UIApplication sharedApplication].keyWindow addSubview:self];
    [UIView animateWithDuration:.3 animations:^{
       
        [self layoutIfNeeded];
    }];
}
-(void)dismissView {
    
    [UIView animateWithDuration:.3 animations:^{
        
        [self layoutIfNeeded];
    } completion:^(BOOL finished) {
        [self.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];
        [self removeFromSuperview];
    }];
}

@end
